{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# JSON [教程](https://www.runoob.com/json/json-tutorial.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 什么是 JSON ？\n",
    "- JSON 指的是 JavaScript 对象表示法（JavaScript Object Notation）\n",
    "- JSON 是轻量级的文本数据交换格式\n",
    "- JSON 独立于语言：JSON 使用 Javascript语法来描述数据对象，但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的动态（PHP，JSP，.NET）编程语言都支持JSON。\n",
    "- JSON 具有自我描述性，更易理解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON - 转换为 JavaScript 对象\n",
    "JSON 文本格式在语法上与创建 JavaScript 对象的代码相同。\n",
    "\n",
    "由于这种相似性，无需解析器，JavaScript 程序能够使用内建的 eval() 函数，用 JSON 数据来生成原生的 JavaScript 对象。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# JSON 语法\n",
    "JSON 语法是 JavaScript 语法的子集。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON 语法规则\n",
    "JSON 语法是 JavaScript 对象表示语法的子集。\n",
    "\n",
    "- 数据在名称/值对中\n",
    "- 数据由逗号分隔\n",
    "- 大括号保存对象\n",
    "- 中括号保存数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON 值\n",
    "JSON 值可以是：\n",
    "\n",
    "- 数字（整数或浮点数）\n",
    "- 字符串（在双引号中）\n",
    "- 逻辑值（true 或 false）\n",
    "- 数组（在中括号中）\n",
    "- 对象（在大括号中）\n",
    "- null"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 相关函数\n",
    "|函数|\t描述|\n",
    "|:---|:-------|\n",
    "|JSON.parse()|\t用于将一个 JSON 字符串转换为 JavaScript 对象。|\n",
    "|JSON.stringify()|\t用于将 JavaScript 值转换为 JSON 字符串。|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON 字符串转换为 JavaScript 对象\n",
    "通常我们从服务器中读取 JSON 数据，并在网页中显示数据。\n",
    "\n",
    "可使用 JavaScript 内置函数 JSON.parse() 将字符串转换为 JavaScript 对象\n",
    "\n",
    "### **[语法](https://www.runoob.com/js/javascript-json-parse.html)**  \n",
    "`JSON.parse(text[, reviver])`\n",
    "**参数说明：**\n",
    "- **text**:必需， 一个有效的 JSON 字符串。\n",
    "- **reviver**: 可选，一个转换结果的函数， 将为对象的每个成员调用此函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "string\n",
      "{\n",
      "  employees: [\n",
      "    { name: '菜鸟教程', site: 'http://www.runoob.com' },\n",
      "    { name: 'Google', site: 'http://www.Google.com' },\n",
      "    { name: 'Taobao', site: 'http://www.taobao.com' }\n",
      "  ]\n",
      "}\n",
      "Google\n"
     ]
    }
   ],
   "source": [
    "var text = '{\"employees\":[' +\n",
    "    '{\"name\":\"菜鸟教程\",\"site\":\"http://www.runoob.com\" },' +\n",
    "    '{\"name\":\"Google\",\"site\":\"http://www.Google.com\" },' +\n",
    "    '{\"name\":\"Taobao\",\"site\":\"http://www.taobao.com\" }]}';\n",
    "console.log(typeof(text))\n",
    "\n",
    "obj = JSON.parse(text);\n",
    "console.log(obj)\n",
    "console.log(obj.employees[1].name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "4\n",
      "6\n",
      "5\n",
      "3\n",
      "\n",
      "{ '6': 6 }\n"
     ]
    }
   ],
   "source": [
    "var text = '{\"1\": 1, \"2\": 2, \"3\": {\"4\": 4, \"5\": {\"6\": 6}}}';\n",
    "\n",
    "obj = JSON.parse(text, function(k, v) {\n",
    "  console.log( k );// 输出当前属性，最后一个为 \"\"\n",
    "  return v;       // 返回修改的值\n",
    "});\n",
    "console.log(obj[3]['5'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JSON.stringify() 将值转换为 JSON 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [语法](https://www.runoob.com/js/javascript-json-stringify.html)\n",
    "`JSON.stringify(value[, replacer[, space]])`\n",
    "\n",
    "**参数说明：**  \n",
    "\n",
    "**value:**  \n",
    "必需， 要转换的 JavaScript 值（通常为对象或数组）。\n",
    "\n",
    "**replacer:**  \n",
    "可选。用于转换结果的函数或数组。\n",
    "\n",
    "如果 replacer 为函数，则 JSON.stringify 将调用该函数，并传入每个成员的键和值。使用返回值而不是原始值。如果此函数返回 undefined，则排除成员。根对象的键是一个空字符串：\"\"。\n",
    "\n",
    "如果 replacer 是一个数组，则仅转换该数组中具有键值的成员。成员的转换顺序与键在数组中的顺序一样。\n",
    "\n",
    "**space:**  \n",
    "可选，文本添加缩进、空格和换行符，如果 space 是一个数字，则返回值文本在每个级别缩进指定数目的空格，如果 space 大于 10，则文本缩进 10 个空格。space 也可以使用非数字，如：\\t。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"name\":\"菜鸟教程\",\"site\":\"http://www.runoob.com\"}\n",
      "{\n",
      "    \"name\": \"菜鸟教程\",\n",
      "    \"site\": \"http://www.runoob.com\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "var str_pretty1 = {\"name\":\"菜鸟教程\", \"site\":\"http://www.runoob.com\"}\n",
    "\n",
    "str_pretty1 = JSON.stringify(str)\n",
    "console.log(str_pretty1)\n",
    "\n",
    "str_pretty2 = JSON.stringify(str, null, 4) //使用四个空格缩进\n",
    "console.log(str_pretty2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Javascript (Node.js)",
   "language": "javascript",
   "name": "javascript"
  },
  "language_info": {
   "file_extension": ".js",
   "mimetype": "application/javascript",
   "name": "javascript",
   "version": "13.13.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
